﻿using UnityEngine;
using System.Collections;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
namespace UEngine
{
    public class DebugHelper
    {
        public static void MessageBox(string message)
        {
            GameObject UIRootGo = GameObject.FindWithTag("UIRoot");
            if (UIRootGo == null)
            {
                return;
            }

            //先看下UIROOT下有没有挂，没有就从资源文件夹中创建一个
            GameObject UIMessageBoxGo = null;
            Transform UIMessageBoxtrs = UIRootGo.transform.Find("Panel_mssagebox");
            if (UIMessageBoxtrs == null)
            {
                UIMessageBoxGo = GameObject.Instantiate(Resources.Load("Debug/Panel_mssagebox", typeof(GameObject))) as GameObject;
                if (UIMessageBoxGo == null)
                {
                    return;
                }
            }

            //挂到UI根节点下
            RectTransform UIRootTransform = UIRootGo.GetComponent<RectTransform>();
            UIMessageBoxGo.transform.SetParent(UIRootTransform, false);

            //如果没有脚本添加脚本组件
            MessageBoxScript messageBoxScript = UIMessageBoxGo.GetComponent<MessageBoxScript>();
            if (messageBoxScript == null)
            {
                messageBoxScript = UIMessageBoxGo.AddComponent<MessageBoxScript>();
            }

            messageBoxScript.ShowMessage(message);

        }


        public static void ErrorMessage(string message)
        {
            if (!StringHelper.IsValidString(message)) return;

            //这个写法只能显示出函数名，其他都不行
            //StackTrace stackTrace = new StackTrace();
            //StackFrame stackFrame = stackTrace.GetFrame(1);
            //message += string.Format("File: {0}", stackFrame.GetFileName());
            //message += string.Format("Method: {0}", stackFrame.GetMethod());
            //message += string.Format("Line Number: {0}", stackFrame.GetFileLineNumber());
            //message += string.Format("Column Number: {0}", stackFrame.GetFileColumnNumber());
            //Debug.Log(message);

            //这个写法可以追溯到堆栈调用过程
            //StackTrace stackTrace = new StackTrace(1, true);
            //StackFrame[] stackFrames = stackTrace.GetFrames();
            //foreach (StackFrame stackFrame in stackFrames)
            //{
            //    Debug.Log(stackFrame.GetMethod());
            //    Debug.Log(stackFrame.GetFileName());
            //    Debug.Log(stackFrame.GetFileLineNumber());
            //    Debug.Log(stackFrame.GetFileColumnNumber());
            //}

            string debugMessage = message;
            StackTrace stackTrace = new StackTrace(1, true);
            StackFrame stackFrame = stackTrace.GetFrame(0);
            debugMessage += string.Format("\n FileName: {0}", stackFrame.GetFileName());
            debugMessage += string.Format("\n Method: {0}", stackFrame.GetMethod());
            debugMessage += string.Format("\n LineNumber: {0}", stackFrame.GetFileLineNumber());
            debugMessage += string.Format("\n ColumnNumber: {0}", stackFrame.GetFileColumnNumber());
            Debug.Log(debugMessage);
            //MessageBox(debugMessage);
            DebugMessageBox(debugMessage);
        }

        public static void DebugMessageBox(string message)
        {
            GameObject UIRootGo = GameObject.FindWithTag("UIRoot");
            if (UIRootGo == null)
            {
                
                return;
            }

            //先看下UIROOT下有没有挂，没有就从资源文件夹中创建一个
            GameObject UIMessageBoxGo = null;
            Transform UIMessageBoxtrs = UIRootGo.transform.Find("Panel_DebugMessageBox(Clone)");
            if (UIMessageBoxtrs == null)
            {
                Debug.Log("Cann't find Panel_DebugMessageBox , so crate it");
                UIMessageBoxGo = GameObject.Instantiate(Resources.Load("Debug/Panel_DebugMessageBox", typeof(GameObject))) as GameObject;
                if (UIMessageBoxGo == null)
                {
                    return;
                }
            }
            else
            {
                UIMessageBoxGo = UIMessageBoxtrs.gameObject;
            }

            //挂到UI根节点下
            RectTransform UIRootTransform = UIRootGo.GetComponent<RectTransform>();
            UIMessageBoxGo.transform.SetParent(UIRootTransform, false);

            //如果没有脚本添加脚本组件
            DebugMessageBoxCom messageBoxScript = UIMessageBoxGo.GetComponent<DebugMessageBoxCom>();
            if (messageBoxScript == null)
            {
                messageBoxScript = UIMessageBoxGo.AddComponent<DebugMessageBoxCom>();
            }

            messageBoxScript.ShowMessage(message);

        }

    }
    
}


